package stark.app.function.translate;

import android.text.TextUtils;
import android.util.Log;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.translator.TranslateActivity;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityTranslateDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.base.IReqRetCallback;
import stark.common.other.LanCode;
import stark.common.other.ReqManager;
import stark.common.other.bean.TranslateRet;

/**
 * Author：daizhongbin on 2021/6/23
 * Email：1027154338@qq.com
 * Desc: 翻译模块示例界面
 */
public class TranslateDemoActivity extends BaseNoModelActivity<ActivityTranslateDemoBinding> {
    @Override
    protected int onCreate() {
        return R.layout.activity_translate_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnDefTranslate.setOnClickListener(v -> {
            TranslateActivity.start(this, "你好世界");
        });
        mDataBinding.btnTranEn.setOnClickListener(v -> {
            translateTo(LanCode.EN);
        });
        mDataBinding.btnTranJapan.setOnClickListener(v -> {
            translateTo(LanCode.JP);
        });
    }

    @Override
    protected void initData() {
    }

    private void translateTo(LanCode toLanCode) {
        String content = mDataBinding.etContent.getText().toString();
        if (TextUtils.isEmpty(content)) {
            mDataBinding.etContent.requestFocus();
            ToastUtils.showShort("请先输入要翻译的文本");
            return;
        }
        showDialog("翻译中...");
        Log.e("dzb", "待翻译内容：" + mDataBinding.etContent.getText().toString());
        ReqManager.getInstance().translate(content, LanCode.AUTO, toLanCode, new IReqRetCallback<TranslateRet>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable TranslateRet result) {
                dismissDialog();
                if (result == null) {
                    ToastUtils.showShort(msg);
                    return;
                }
                mDataBinding.tvRet.setText(result.getResult());
            }
        });
    }
}
